<?php

namespace App\V3\V3_2\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class HcMaterielTypeModel extends Model
{
    use HasFactory;

    protected $table = 'hc_materiel_type';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }


    //获取指定商品详细信息
    static public function info($id)
    {
        return self::select(
            'a.id',
            'a.materiel_name',
            'a.type_name',
            'a.materiel_img',
            'a.materiel_no',
            'a.materiel_class_id',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.last_cost',
            'a.selling_price',
            "a.is_album",
            'a.lower_selling_price',
            'a.materiel_unit'
            )
            ->from('hc_materiel_type as a')
            ->leftJoin('hc_materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('hc_materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where('a.id', $id)
            ->where('a.delete_flag', 0)
            ->first();
    }


    //物料商品列表
    static public function materielTypeSelect($mainId, $keyword, $condition,$nextClassIdArr, $pageSize, $currentPage)
    {
        $query= self::select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_img',
            'materiel_no',
            'materiel_class_id',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.lower_selling_price',
            'a.materiel_unit',
            'a.is_album'
            )
            ->from('hc_materiel_type as a')
            ->leftJoin('hc_materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('hc_materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where($condition)
           /*  ->where(function($query) use ($keyword) {
                $query->orWhere('a.materiel_name', 'like', '%'.$keyword.'%');
                $query->orWhere('a.type_name', 'like', '%'.$keyword.'%');
                $query->orWhere('a.materiel_no', 'like', '%'.$keyword.'%');
                $query->orWhere('b.class_name', 'like', '%'.$keyword.'%');
                $query->orWhere('c.class_name', 'like', '%'.$keyword.'%');
            }) */
            ->where('a.delete_flag', 0)
            ->where('a.main_id', $mainId);
            if(!empty($nextClassIdArr)){
             $query->whereIn('a.materiel_class_id',$nextClassIdArr);
            }
           return $query->orderBy('a.id', 'DESC')
            ->paginate($pageSize,['*'], '',$currentPage)->toArray();
    }


  static public function add($data){

     return HcMaterielTypeModel::insertGetId($data);
  }

  //商品编号查重-修改
  static public function checkMaterielTypeCode($data)
  {
      return self::where([
          'materiel_no' => $data['materiel_no'],
          'main_id' => $data['main_id'],
          'delete_flag' => 0
      ])->where('id', '!=', $data['id'])->first();
  }
}
